libcrun: check setenv failure#2007
Conversation
Summary of ChangesHello @eriksjolund, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness of Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds necessary error checking for setenv calls, which improves the robustness of the code. The changes in src/libcrun/container.c are correct. However, the modifications in src/libcrun/utils.c to the set_home_env function introduce a bug in the error handling logic. My review includes a comment detailing this issue.
src/libcrun/utils.c
Outdated
| if (UNLIKELY(ret < 0)) | ||
| goto error; | ||
| else | ||
| return 0; |
There was a problem hiding this comment.
The else statement is redundant and can be removed for clarity.
More critically, the reuse of the ret variable introduces a bug. The set_home_env function now incorrectly returns 0 (success) when a user is not found in /etc/passwd. This happens because fgetpwent_r sets ret to 0 on success, and if the user isn't found, the function proceeds to the error label where ret ? -errno : 0 evaluates to 0.
The original logic of returning -1 from the error label was correct for all failure cases, including 'user not found'. The new logic return ret ? -errno : 0; is flawed and should be reconsidered.
if (UNLIKELY(ret < 0))
goto error;
return 0;There was a problem hiding this comment.
this should be addressed, ret is used after the error label.
There was a problem hiding this comment.
Do you know why there is a ternary operator on line 1486 in the current main branch?
It seems ret could never be 0
Here is code from the current main branch.
Lines 1450 to 1487 in 0bc3050
There was a problem hiding this comment.
I tried another approach in
3d23abe
If you like it, I could switch to that.
(Untested code. I haven't run clang-format yet)
There was a problem hiding this comment.
@eriksjolund I like the approach of not reusing ret for setenv checks.
Yet in 3d23abe you also refactor the set_home_env, which seems kind of out of scope for this PR. If you want to refactor it, please do so in a separate commit (so you can explain what's going on). Currently, your commit description says "check setenv failure" but you also do some other things, which is confusing.
There was a problem hiding this comment.
@kolyshkin yes, I will need to create separate commits and PR.
To keep better order I created
giuseppe
left a comment
There was a problem hiding this comment.
nit: could you wrap the checks into UNLIKELY?
You'll need to run make clang-format before committing
src/libcrun/utils.c
Outdated
| if (UNLIKELY(ret < 0)) | ||
| goto error; | ||
| else | ||
| return 0; |
There was a problem hiding this comment.
this should be addressed, ret is used after the error label.
5b751b8 to
ffad99c
Compare
|
TMT tests failed. @containers/packit-build please check. |
1 similar comment
|
TMT tests failed. @containers/packit-build please check. |
| setenv ("HOME", ret_pw->pw_dir, 1); | ||
| return 0; | ||
| if (UNLIKELY (setenv ("HOME", ret_pw->pw_dir, 1) < 0)) | ||
| OOM (); |
There was a problem hiding this comment.
can we just set errno and return -1 here?
We may look at better error reporting later, but OOM seems overkill here
There was a problem hiding this comment.
setenv can fail for two reasons: EINVAL (when name is null, or empty, or contains =, which is not the case with $HOME), and ENOMEM. From the quick glance into glibc's stdlib/setenv.c it seems to be true, so OOM seems legit here.
There was a problem hiding this comment.
The above theoretically means we can OOM() in other places after setenv failed, if its first argument is known to be good. Practically, I'm not sure since non-glibc implementations can possibly return other errors (although looking into current musl sources, it's the same as for glibc).
There was a problem hiding this comment.
ok that is good, OOM looks correct. Thanks!
There was a problem hiding this comment.
I haven't had time to look into this, but my original idea by using OOM() was to avoid that
the environment variable HOME is set to / because the system is low on memory, for example here
Lines 3690 to 3696 in c07aadc
Thinking more about it, a similar problem could occur if fopen() fails with ENFILE here
Lines 1457 to 1459 in c07aadc
Maybe set_home_env() should take an err argument so that it is possible for the caller to differentiate between
- no. home path was found in /etc/passwd
- some other problems occured (for instance
ENFILEorENOMEM)
There was a problem hiding this comment.
@eriksjolund this sounds good to me. I'm going to merge this PR as is though, as it fixes the issue #1998.
Feel free to a new PR, improving set_home_env error reporting.
There was a problem hiding this comment.
... and also calling OOM in case setenv failed, in other places, like suggested above.
|
Ah, this needs a rebase to bring in #2044 so CI is green, and then I can merge. @eriksjolund can you please rebase? |
Closes: containers#1998 Signed-off-by: Erik Sjölund <erik.sjolund@gmail.com>
ffad99c to
4db1709
Compare
Closes: #1998